<template>
  <div class="work-calendar">
    <!-- 选择年月的下拉选择框 -->
    <div class="header">
      <el-select v-model="currentYear" size="small" @change="changeDate">
        <el-option v-for="item in yearList" :key="item" :label="item" :value="item" />
      </el-select>
      <!-- 月份 -->
      <el-select v-model="currentMonth" size="small" @change="changeDate">
        <el-option v-for="item in 12" :key="item" :label="item" :value="item" />
      </el-select>
    </div>
    <el-calendar v-model="value">
      <!--  {{ data }} 是一个对象 包含三个字段
       { isSelected: true/false type: 'current-month , day: '2021-11-09' 日期字符串}
       date 是一个日期对象
       -->
      <template #dateCell="{data, date}">
        <!-- {{ getDay(data) }} -->
        <!-- <span v-if="isWeek(date)">休</span> -->
        <!-- <span v-if="isWeek(data)">休</span> -->
        <!-- 美化后的 -->
        <div class="date-content">
          <span class="text">{{ getDay(data) }}</span>
          <span v-if="isWeek(date)" class="rest">休</span>
          <!-- <span v-if="isWeek(data)" class="rest">休</span> -->
        </div>
      </template>
    </el-calendar>
  </div>
</template>

<script>
export default {
  props: {
    // 接收父组件传递的日期
    startDate: {
      type: Date,
      default: () => new Date() // 复杂数据类型的默认值是 工厂函数返回一个结果
    }
  },
  data() {
    return {
      currentMonth: null, // 复选框关联的月份
      currentYear: null, // 复选框关联的年份
      // yearList: [],
      value: null, // 日历组件关联的日期对象
      // 模拟的休息日期列表
      weekList: ['2021-11-03', '2021-11-04', '2021-11-08', '2021-11-12', '2021-11-23']
    }
  },
  computed: {
    // 前五年 + 今年 + 后五年
    yearList() {
      return Array.from(new Array(11), (item, index) => {
        return index - 5 + this.currentYear
      })
    }
  },
  watch: {
    // 监听value值的变化, 将这个日期对象的年和月取出再赋值给 currentYear和currentMonth
    value(newval) { // newval是一个日期对象
      this.currentYear = newval.getFullYear()
      this.currentMonth = newval.getMonth() + 1
    }
  },
  created() {
    this.currentYear = this.startDate.getFullYear()
    this.currentMonth = this.startDate.getMonth() + 1
    this.value = this.startDate
  },
  methods: {
    // 监听两个下拉表单的change事件拿到最新的年和月 生成一个日期对象 设置给 日历组件关联到的value
    changeDate() {
      // console.log(this.currentYear)
      // console.log(this.currentMonth)
      this.value = new Date(`${this.currentYear}-${this.currentMonth}-1`)
    },
    getDay(data) {
      const day = data.day.split('-')[2]
      // startsWith检测字符串是否以某个字符开头,如果是 返回true
      return day.startsWith(0) ? day.slice(1) : day
    },
    // 动态控制周末休息
    isWeek(date) {
      // date.getDay() // 获取到今天是周几  1 - 6 周一-周六  周日是0
      return date.getDay() === 6 || date.getDay() === 0
    }
    // 自定义休息
    /* isWeek(date) {
      return this.weekList.includes(date.day)
    } */
  }
}
</script>

<style lang="scss" scoped>
.work-calendar {
  ::v-deep.el-calendar__header {
    display: none;
  }
  .header{
    display: flex;
    justify-content: flex-end;
    .el-select {
      margin-left: 10px;
    }
  }
}

</style>
<style lang="scss" scoped>
::v-deep .el-calendar-day {
  height:  auto;
 }
::v-deep .el-calendar-table__row td::v-deep .el-calendar-table tr td:first-child, ::v-deep .el-calendar-table__row td.prev{
  border:none;
 }
.date-content {
  height: 40px;
  text-align: center;
  line-height: 40px;
  font-size: 14px;
}
.date-content .rest {
  color: #fff;
  border-radius: 50%;
  background: rgb(250, 124, 77);
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
  font-size: 12px;
  margin-left: 10px;
}
.date-content .text{
  width: 20px;
  height: 20px;
  line-height: 20px;
 display: inline-block;

}
::v-deep .el-calendar-table td.is-selected .text{
   background: #409eff;
   color: #fff;
   border-radius: 50%;
 }
::v-deep .el-calendar__header {
   display: none
 }
</style>
